package com.yanqu.road.server.cmd.activity.huashigang;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.huashigang.HuaShiGangActivityEventConfig;
import com.yanqu.road.entity.enums.activity.huashigang.eHuaShiGangEventType;
import com.yanqu.road.pb.huashigang.HuaShiGangProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.huashigang.HuaShiGangMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.PropertyHelper;

/**
 * @Author wpj
 * @Date 2020/10/6
 */
@Cmd(code = GameProtocol.S_HUA_SHI_GANG_TRADE, desc = "花石纲贸易")
public class TradeCmd extends AbstractCommand {
    int eventId;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        HuaShiGangProto.TradeExchangeReqMsg msg = HuaShiGangProto.TradeExchangeReqMsg.parseFrom(packet.getBytes());
        eventId = msg.getEventId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ret = HuaShiGangMgr.getActivityInfoRet(player);
        if (ret != 0) {
            return;
        }
        ActivityInfo activityInfo = HuaShiGangMgr.getActivityInfo();
        if (!ActivityMgr.activityInTime(activityInfo)) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }

        HuaShiGangActivityEventConfig config = HuaShiGangMgr.getHuaShiGangActivityEventConfigMap().get(eventId);
        if (config == null) {
            ret = GameErrorCode.E_HUA_SHI_GANG_EVENT_ERROR;
            return;
        }
        if (config.getEventType() != eHuaShiGangEventType.TRADE_EVENT.getValue()) {
            ret = GameErrorCode.E_HUA_SHI_GANG_EVENT_ERROR;
            return;
        }

        if (!player.getModule(CurrencyModule.class).currencyIsEnough(PropertyHelper.parseStringToProperty(config.getParamList()))) {
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }

        HuaShiGangProto.CrossTradeExchangeReqMsg.Builder builder = HuaShiGangProto.CrossTradeExchangeReqMsg.newBuilder();
        builder.setActivityId(activityInfo.getActivityId());
        builder.setEventId(eventId);
        player.sendPacket(CrossProtocol.C2_HUA_SHI_GANG_TRADE, builder);

    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            HuaShiGangProto.TradeExchangeRespMsg.Builder builder = HuaShiGangProto.TradeExchangeRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_HUA_SHI_GANG_TRADE, builder);
        }

    }
}
